package server

import (
	"context"
	"log"
	"net/http"
	__ "user_server/basic/proto"
	"user_server/handler/dao"
	"user_server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type UserServer struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *UserServer) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	var user model.User
	if user.Account == in.Account {
		return &__.RegisterResp{
			Message: "此用户已存在",
			Code:    http.StatusBadRequest,
		}, nil
	}
	user = model.User{
		Account:  in.Account,
		PassWord: in.PassWord,
	}
	if !dao.Create(&user) {
		return &__.RegisterResp{
			Message: "注册失败",
			Code:    http.StatusBadRequest,
		}, nil
	}
	return &__.RegisterResp{
		Message: "ok",
		Code:    http.StatusOK,
	}, nil
}

func (s *UserServer) TestInfo(_ context.Context, in *__.TestInfoReq) (*__.TestInfoResp, error) {
	var t model.ExamTest
	dao.GetOneByFields(&model.ExamTest{
		Id:             in.Id,
		Administrators: in.Administrators,
	}, &t)
	if in.Administrators != 1 {
		return &__.TestInfoResp{
			ExamType:                    t.ExamType,
			Difficulty:                  t.Difficulty,
			FirstLevelProfessionalScope: t.FirstLevelProfessionalScope,
			TwoLevelProfessionalScope:   t.TwoLevelProfessionalScope,
			User:                        t.User,
			Title:                       t.Title,
			TestOptions:                 t.TestOptions,
			OptionAnswer:                t.OptionAnswer,
			Analysis:                    t.Analysis,
			Keywords:                    t.Keywords,
			UserId:                      t.UserId,
			CreateUser:                  t.CreateUser,
			CreateUserId:                t.CreateUserId,
			CreateUserName:              t.CreateUserName,
			Status:                      t.Status,
		}, nil
	}
	return &__.TestInfoResp{
		ExamType:                    t.ExamType,
		Difficulty:                  t.Difficulty,
		FirstLevelProfessionalScope: t.FirstLevelProfessionalScope,
		TwoLevelProfessionalScope:   t.TwoLevelProfessionalScope,
		User:                        t.User,
		Title:                       t.Title,
		TestOptions:                 t.TestOptions,
		OptionAnswer:                t.OptionAnswer,
		Analysis:                    t.Analysis,
		Keywords:                    t.Keywords,
		UserId:                      t.UserId,
		CreateUser:                  t.CreateUser,
		CreateUserId:                t.CreateUserId,
		CreateUserName:              t.CreateUserName,
		Status:                      t.Status,
		Reviewer:                    t.Reviewer,
		AuditPersonnelNumber:        t.AuditPersonnelNumber,
	}, nil
}

func (s *UserServer) ExsExam(_ context.Context, in *__.ExsExamReq) (*__.ExsExamResp, error) {
	var e model.ExsExam
	dao.GetOneByFields(&model.ExsExam{Id: in.Id}, &e)
	return &__.ExsExamResp{
		TestPaperType:             e.TestPaperType,
		ExamTitle:                 e.ExamTitle,
		ExamType:                  e.ExamType,
		ExamDuration:              e.ExamDuration,
		OrderOfTestQuestions:      e.OrderOfTestQuestions,
		OrderOfOptionsArrangement: e.OrderOfOptionsArrangement,
		TotalScore:                e.TotalScore,
		PassingMark:               e.PassingMark,
	}, nil
}

func (s *UserServer) Examination(_ context.Context, in *__.ExaminationReq) (*__.ExaminationResp, error) {
	var e model.Examination
	dao.GetOneByFields(&model.Examination{Id: in.Id}, &e)
	if in.ExamStatus == 3 {
		log.Panicln("考试还未开始")
		return nil, nil
	} else if in.ExamStatus == 1 {
		log.Panicln("考试还未开始")
		return nil, nil
	}

	return &__.ExaminationResp{
		ProjectTitle:      e.ProjectTitle,
		ExamTitle:         e.ExamTitle,
		ProfessionalScope: e.ProfessionalScope,
		Status:            e.Status,
		User:              e.User,
	}, nil
}

func (s *UserServer) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var u model.User
	if !dao.GetOneByFields(&model.User{Account: in.Account, PassWord: in.PassWord}, &u) {
		return &__.LoginResp{
			Message: "没查询到此用户",
			Code:    http.StatusBadRequest,
		}, nil
	}
	if u.PassWord != in.PassWord {
		return &__.LoginResp{
			Message: "密码错误，请重新输入",
			Code:    http.StatusBadRequest,
		}, nil
	}
	return &__.LoginResp{
		Message:  "ok",
		Code:     http.StatusOK,
		Account:  u.Account,
		PassWord: u.PassWord,
	}, nil
}
